local weijing = fk.CreateSkill {
  name = "lingling__weijingh",
}

Fk:loadTranslationTable {
  ["lingling__weijingh"] = "未竟",
  [":lingling__weijingh"] = "当你不以此法从某处获得牌后，你可以从该处再获得一张牌。若不为你本轮前两次发动，你弃置一张牌。",

  ["#lingling__weijingh1-invoke"] = "未竟：是否再获得 %dest %arg一张牌？",
  ["#lingling__weijingh2-invoke"] = "未竟：是否再从 %arg 获得一张牌？",
}

local trans = {
  [Card.PlayerHand] = "$Hand",
  [Card.PlayerEquip] = "$Equip",
  [Card.PlayerJudge] = "$Judge",
  [Card.PlayerSpecial] = "",

  [Card.DrawPile] = "pile_draw",
  [Card.DiscardPile] = "pile_discard",
  [Card.Processing] = "processing_area",
  [Card.Void] = "pile_draw",
}

weijing:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  trigger_times = function(self, event, target, player, data)
    return 1
  end,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(weijing.name) then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand and move.skillName ~= weijing.name then
          if move.from then
            if not move.from.dead then
              for _, info in ipairs(move.moveInfo) do
                if #move.from:getCardIds(info.fromArea) > 0 then
                  return true
                end
              end
            end
          else
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.DrawPile and #player.room.draw_pile > 0 then
                return true
              elseif info.fromArea == Card.DiscardPile and #player.room.discard_pile > 0 then
                return true
              elseif info.fromArea == Card.Processing and #player.room.processing_area > 0 then
                return true
              elseif info.fromArea == Card.Void and #player.room.void > 0 then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    local dat = {}
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand and move.skillName ~= weijing.name then
        if move.from then
          if not move.from.dead then
            for _, info in ipairs(move.moveInfo) do
              if #move.from:getCardIds(info.fromArea) > 0 then
                dat[info.cardId] = { move.from, info.fromArea }
                break
              end
            end
          end
        else
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.DrawPile and #player.room.draw_pile > 0 then
              dat[info.cardId] = { nil, info.fromArea }
              break
            elseif info.fromArea == Card.DiscardPile and #player.room.discard_pile > 0 then
              dat[info.cardId] = { nil, info.fromArea }
              break
            elseif info.fromArea == Card.Processing and #player.room.processing_area > 0 then
              dat[info.cardId] = { nil, info.fromArea }
              break
            elseif info.fromArea == Card.Void and #player.room.void > 0 then
              dat[info.cardId] = { nil, info.fromArea }
              break
            end
          end
        end
      end
    end
    for _, d in pairs(dat) do
      if not player:hasSkill(weijing.name) then return end
      if d[1] then
        if #d[1]:getCardIds(d[2]) > 0 then
          event:setCostData(self, { choice = 1, tos = { d[1] }, area = d[2] })
          self:doCost(event, target, player, data)
        end
      else
        local mapper = {
          [Card.DrawPile] = room.draw_pile,
          [Card.DiscardPile] = room.discard_pile,
          [Card.Processing] = room.processing_area,
          [Card.Void] = room.void,
        }
        if #mapper[d[2]] > 0 then
          event:setCostData(self, { choice = 2, tos = {}, area = d[2] })
          self:doCost(event, target, player, data)
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choice = event:getCostData(self).choice
    local area = event:getCostData(self).area
    if choice == 1 then
      local to = event:getCostData(self).tos[1]
      if room:askToSkillInvoke(player, {
            skill_name = weijing.name,
            prompt = "#lingling__weijingh1-invoke::" .. to.id .. ":" .. trans[area],
          }) then
        event:setCostData(self, { tos = { to }, choice = area })
        return true
      end
    else
      if room:askToSkillInvoke(player, {
            skill_name = weijing.name,
            prompt = "#lingling__weijingh2-invoke:::" .. trans[area],
          }) then
        event:setCostData(self, { choice = area })
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local area = event:getCostData(self).choice
    if event:getCostData(self).tos then
      local to = event:getCostData(self).tos[1]
      local cards = to:getCardIds(area)
      room:moveCardTo(table.random(cards), Card.PlayerHand, player, fk.ReasonPrey, weijing.name, nil, false, player)
    else
      local mapper = {
        [Card.DrawPile] = room.draw_pile,
        [Card.DiscardPile] = room.discard_pile,
        [Card.Processing] = room.processing_area,
        [Card.Void] = room.void,
      }
      local cards = mapper[area]
      room:moveCardTo(table.random(cards), Card.PlayerHand, player, fk.ReasonJustMove, weijing.name, nil, false, player)
    end
    if not player.dead and player:usedSkillTimes(weijing.name, Player.HistoryRound) > 2 then
      room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = weijing.name,
        cancelable = false,
      })
    end
  end,
})

return weijing
